Skip to content

internal: merge Trait and TraitAlias handling #20376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2025

Conversation

fee1-dead
Copy link
Member

Much of the current infrastructure around traits are good and cool but trait aliases don't get to enjoy type inference because they are treated as distinct entities from traits.

Doesn't seem easy to change all the existing facilities to accept something like Either<Trait, TraitAlias>.

We can actually just merge them and most things will work. Second commit adds a simple test.

The core of this change is in crates/syntax/rust.ungram.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2025
@fee1-dead fee1-dead changed the title Merge Trait and TraitAlias handing Merge Trait and TraitAlias handling Aug 4, 2025
@fee1-dead fee1-dead force-pushed the traitalias branch 2 times, most recently from 900a128 to e3cf7eb Compare August 4, 2025 08:29
let ast_id_map = db.ast_id_map(ast_id.file_id);
let source = ast_id.with_value(ast_id_map.get(ast_id.value)).to_node(db);
if source.eq_token().is_some() {
// FIXME(trait-alias) probably needs special handling here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this would be okay as this PR treats TraitAliass somewhat like new trait with bounds and no assoc items and rust-analyzer won't demand assoc items for such traits or (currently for) trait aliases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I reconsidered and thought it might not need special handling halfway into implementing this PR. I originally was thinking maybe we should return the associated items of the trait that it aliases but we already lower them with bound handling - so the aliased trait will be treated as a super trait

Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a solid improvement overall. It might end up marking trait aliases as actual traits, but it correctly resolves associated types and, more importantly, trait methods. That alone could potentially address our long-standing issues with trait aliases, such as #15566 .

My only hesitation is whether this could complicate the merging of #20329. I skimmed that PR and only noticed a few minor references to TraitAlias, so the impact may be limited.

That said, I'm unsure whether we should merge this before or after #20329. cc @rust-lang/rust-analyzer

@fee1-dead
Copy link
Member Author

#20329 was merged already and GitHub does not report any merge conflicts with this PR - so I think this is good to go?

@Veykril
Copy link
Member

Veykril commented Aug 13, 2025

Surprising that there are no conflicts, but in that case yes let's merge this 👍

@Veykril Veykril added this pull request to the merge queue Aug 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 13, 2025
@fee1-dead
Copy link
Member Author

Rebased, only the newest commit has changed, please review :)

@Veykril Veykril added this pull request to the merge queue Aug 13, 2025
Merged via the queue into rust-lang:master with commit ac9d0f8 Aug 13, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 13, 2025
@fee1-dead fee1-dead deleted the traitalias branch August 13, 2025 15:25
@lnicola lnicola changed the title Merge Trait and TraitAlias handling internal: merge Trait and TraitAlias handling Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants